package com.nulldev.util.threading.legacy;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class ThreadExecutor {

	private static final ArrayList<Thread> THREADS_LOCAL = new ArrayList<Thread>();
	private static final ThreadPoolExecutor local = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), new ThreadFactory() {

		@Override
		public Thread newThread(Runnable arg0) {
			final Thread tr = new Thread(arg0, "ThreadExecutor: Execution Thread");
			THREADS_LOCAL.add(tr);
			return tr;
		}
	}) {

		public ArrayList<Thread> getThreads(){
			return THREADS_LOCAL;
		}

		@Override
		public void execute(final Runnable r) {
			super.execute(r);
		}
	};
	//private static final ExecutorService shared = Executors.newWorkStealingPool(4);

	public static final void execute(Runnable r) {
		local.execute(r);
	}

	//public static final void executeShared(Runnable r) {
	//	shared.execute(r);
	//}

	public static ThreadPoolExecutor get() {
		return local;
	}

    public static <E> ArrayDeque<E> newArrayDeque() {
        return new ArrayDeque<E>();
    }
    
    public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue() {
        return new ConcurrentLinkedQueue<E>();
    }

}
